# -*- coding: utf-8 -*-

"""
Created on Fri Jun  8 18:43:24 2018

@author: Administrator
"""
from  agilepipe.origin.shellscript import Shell_script
import copy
import os
import pandas as pd
#%%

class Anova_1way_nlevel_mat(Shell_script):
    
    zero_str = '0 '
    one_str = '1 '
    neg_one_str = '-1 '
    design_str = 'design'
    
    def initialize_input_list(self,input_list):
        
        self.level = input_list[0]
        self.group_num_list = input_list[1]
        self.cov_excel_filepath = input_list[2]
        self.cov_column_name_list = input_list[3]
        self.output_dirpath = input_list[4]
        
        if not os.path.exists(self.output_dirpath):
            os.makedirs(self.output_dirpath)
        
    def concat_inputs(self):
        zero_list = self.zero_str * self.level
        one_list = self.one_str * (self.level-1)
        cov_df = pd.read_excel(self.cov_excel_filepath)[self.cov_column_name_list]
        
        
        self.mat_str = self.mat(zero_list,cov_df)
        self.con_str = self.con(zero_list)
        self.fts_str = self.fts(one_list)
        
        self.designmattxt_filename =  self.design_str + self.mat_str.replace('.','') + self.txt_str
        self.designmat_filename = self.design_str + self.mat_str
        
        self.designcontxt_filename =  self.design_str + self.con_str.replace('.','') + self.txt_str
        self.designcon_filename = self.design_str + self.con_str
        
        self.designftstxt_filename =  self.design_str + self.fts_str.replace('.','') + self.txt_str
        self.designfts_filename = self.design_str + self.fts_str
        
    def concat_script(self):
        #cmd_1的内容：cd input_filepath
        self.cmd_1 =  r' cd ' + self.output_dirpath
        
        self.cmd_2 = r' echo ' + self.mat_str + r' > ' + self.designmattxt_filename
        #  Text2Vest design.txt design.mat
        self.cmd_3 = r' Text2Vest ' + self.designmattxt_filename + self.space + self.designmat_filepath

        self.cmd_4 = r' echo ' + self.mat_str + r' > ' + self.designcontxt_filename
        #  Text2Vest design.txt design.mat
        self.cmd_5 = r' Text2Vest ' + self.designcontxt_filename + self.space + self.designcon_filename

        
        self.cmd_6 = r' echo ' + self.mat_str + r' > ' + self.designftstxt_filename 
        #  Text2Vest design.txt design.mat
        self.cmd_7 = r' Text2Vest ' + self.designftstxt_filename  + self.space + self.designfts_filename 

        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 \
                            + self.cmd_sep + self.cmd_4 + self.cmd_sep + self.cmd_5 \
                            + self.cmd_sep + self.cmd_6 + self.cmd_sep + self.cmd_7
 
       
    def mat(self,zero_list,cov_df):
        mat_str = ''
        cov_df_index = cov_df.index
        for i in range(0,self.level):
            raw_list = copy.deepcopy(zero_list)
            raw_list[i] = self.one_str
            raw_list = raw_list + [cov_df_index[cov_df_index[i]]] + [self.newline_mark]
            mat_str = mat_str + ''.join(raw_list*self.group_num_list[i])
        return mat_str
    
    def con(self,zero_list):
        con_str = []
        for i in range(0,self.level-1):
            raw_list = copy.deepcopy(zero_list)
            raw_list[self.level-1] = self.neg_one_str
            raw_list[i] = self.one_str
            raw_list = raw_list + [self.newline_mark]
            con_str = con_str + ''.join(raw_list)
        return con_str
    
    def fts(self,one_list):
        fts_str = ''.join(one_list)
        return fts_str